package jp.ac.aiit.spg.web.todo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;

import org.seasar.teeda.extension.annotation.convert.DateTimeConverter;
import org.seasar.teeda.extension.annotation.validator.Required;

import jp.ac.aiit.spg.AbstractSpgPage;
import jp.ac.aiit.spg.common.SpgAssignStatus;
import jp.ac.aiit.spg.entity.SpgTask;
import jp.ac.aiit.spg.service.SpgService;
import jp.ac.aiit.spg.web.wbs.ConfirmTaskPage;
import jp.ac.aiit.spg.web.wbs.SpgTaskTemplete;

public class UpdateAssignStatementPage extends AbstractSpgPage {
	
	@EJB
	private SpgService ss;
	private SpgTask task;
	private String projectName;
	private String wbsNumber;
	private String taskName;
	private int workingTime;
	@DateTimeConverter(pattern = "yyyy/MM/dd")
	private Date startTimePlanned;
	@DateTimeConverter(pattern = "yyyy/MM/dd")
	private Date finishTimePlanned;
	@Required
	private int assignStatusid;
	private List<Map> assignStatusidItems;
	private String assignMemo;
	private int taskId;

	public Class doUpdate() {
		// タスク作成テンプレートを使って値をサービスにわたす。

		task = ss.getTask(taskId);
		switch (assignStatusid) {
		case 2:
			task.setAssignment(SpgAssignStatus.ACCEPTED);
			break;
		case 3:
			task.setAssignment(SpgAssignStatus.REJECTED);
			break;
		}
		
		if(assignMemo !=null){
			task.setAssignMemo(assignMemo);
		}
		ss.updateTask(task);

		return ConfirmAssignStatementPage.class;
	}

	public Class initialize() {
		return null;
	}

	public Class prerender() {
	
		task = ss.getTask(taskId);

		// タスクの属性を画面にセット
		projectName = task.getWbs().getProject().getName();
		taskName = task.getName();
		wbsNumber = task.getWbsNumber();
		workingTime = task.getWorkingTime();
		startTimePlanned = task.getStartTimePlanned();
		finishTimePlanned = task.getFinishTimePlanned();
		assignMemo = task.getAssignMemo();

		// 承認状態の選択肢を表示コンポーネントに詰める
		// 現行の値もセットする。
		prepareAssignOptions();

		return null;
	}

	private void prepareAssignOptions() {
		
		int assignId = converterAssignId(task);
		// 承認状態の選択肢を表示コンポーネントに詰める
		assignStatusidItems = new ArrayList<Map>();
		Map map1 = new HashMap();
		map1.put("label", " ");
		map1.put("value", new Integer(1));
		assignStatusidItems.add(map1);	
		
		Map map2 = new HashMap();
		map2.put("label", SpgAssignStatus.ACCEPTED.toString());
		map2.put("value", new Integer(2));
		assignStatusidItems.add(map2);

		Map map3 = new HashMap();
		map3.put("label", SpgAssignStatus.REJECTED.toString());
		map3.put("value", new Integer(3));
		assignStatusidItems.add(map3);
		
		// 承認状態のデフォルトの選択肢を設定。
		assignStatusid = assignId;
	}
	
	private int converterAssignId(SpgTask task){
		
		int assignId = 1;
		if(task.getAssignment()==SpgAssignStatus.ACCEPTED){
			assignId = 2;
		}else if(task.getAssignment()==SpgAssignStatus.REJECTED){
			assignId = 3;
		};
		
		return assignId;
	}
	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getWbsNumber() {
		return wbsNumber;
	}

	public void setWbsNumber(String wbsNumber) {
		this.wbsNumber = wbsNumber;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public int getWorkingTime() {
		return workingTime;
	}

	public void setWorkingTime(int workingTime) {
		this.workingTime = workingTime;
	}

	public Date getStartTimePlanned() {
		return startTimePlanned;
	}

	public void setStartTimePlanned(Date startTimePlanned) {
		this.startTimePlanned = startTimePlanned;
	}

	public Date getFinishTimePlanned() {
		return finishTimePlanned;
	}

	public void setFinishTimePlanned(Date finishTimePlanned) {
		this.finishTimePlanned = finishTimePlanned;
	}

	public int getAssignStatusid() {
		return assignStatusid;
	}

	public void setAssignStatusid(int assignStatusid) {
		this.assignStatusid = assignStatusid;
	}

	public List<Map> getAssignStatusidItems() {
		return assignStatusidItems;
	}

	public void setAssignStatusidItems(List<Map> assignStatusidItems) {
		this.assignStatusidItems = assignStatusidItems;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

	public String getAssignMemo() {
		return assignMemo;
	}

	public void setAssignMemo(String assignMemo) {
		this.assignMemo = assignMemo;
	}

}
